package com.syedu.cels.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.syedu.cels.dao.BaseDao;
import com.syedu.cels.entity.*;
import com.syedu.cels.exception.VocabularyFileNotFoundException;
import com.syedu.cels.exception.WordFileNotFoundException;
import com.syedu.cels.util.BeanFactory;
import com.syedu.cels.util.InputUtil;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.syedu.cels.constants.Iconstants.TEST_RESULT;
import static com.syedu.cels.constants.Iconstants.WRONG_TEST;

/**
 * @author easonsy
 * @Description
 * @createTime 2022/03/21 23:37:00
 */
public class BaseService {
    BaseDao dao=(BaseDao) BeanFactory.getBean("baseDao");
    public List<Word> findWordsByPage(String firstLetter,int pageNow,int pageSize){
        Map<Character,List<Word>> wordsMap=getWordsMap();
        List<Word> words=wordsMap.get(firstLetter.charAt(0));
        return CollUtil.page(pageNow-1,pageSize,words);
    }
    public List<Vocabulary> findVocabulariesByPage(int pageNow, int pageSize){
        List<Vocabulary> list=null;
        try {
            list=CollUtil.page(pageNow-1,pageSize,dao.selectAllVocabularies());
        } catch (VocabularyFileNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }
    public Map<Character,List<Word>> getWordsMap(){
        List<Word> words= null;
        Map<Character,List<Word>> map=null;
        try {
            words = dao.selectAllWord();
        if(words!=null&words.size()>0){
            map=new HashMap<>();
           for(Word word:words){
               if(map.containsKey(word.getFirstletter())){
               map.get(word.getFirstletter()).add(word);}
               else{
                   List<Word> subList=new ArrayList<>();
                   subList.add(word);
                   map.put(word.getFirstletter(),subList);
               }
           }
        }} catch (WordFileNotFoundException e) {
            e.printStackTrace();
        }
        return map;
    }
    public int vocabularyCount(){
        int count=0;
        try {
            count= dao.selectAllVocabularies().size();
        } catch (VocabularyFileNotFoundException e) {
            e.printStackTrace();
        }
        return count;
    }
    public List<Exam> TestExam(){
       return dao.generateTestPaper().subList(0,10);

}
public void storeResult(int rightCount){
        String comment=null;
        if(rightCount>=6){
            comment="及格";
        }else{
            comment="不及格";
        }
    Result result=new Result(10,rightCount,10-rightCount,rightCount / 10.0,comment, LocalDateTime.now());
        dao.collectResult(result);
}
    public void showResult() {
        try {
            List<String>  strings = Files.readAllLines(Path.of(TEST_RESULT));
        List<Result> list=new ArrayList<>();
        for(String s:strings){
            list.add(JSON.parseObject(s,Result.class));
        }
        list.forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void wrongTest(Wrong wrong){
        try (PrintWriter printWriter = new PrintWriter(new FileWriter(WRONG_TEST, true), true)) {
            printWriter.println(JSON.toJSONString(wrong));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void showWrong() {
        try {
            List<String>  strings = Files.readAllLines(Path.of(WRONG_TEST));
            List<Wrong> list=new ArrayList<>();
            for(String s:strings){
                list.add(JSON.parseObject(s,Wrong.class));
            }
            list.forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void showWrongTest() {
        try {
            List<String>  strings = Files.readAllLines(Path.of(WRONG_TEST));
            List<Wrong> list=new ArrayList<>();
            for(String s:strings){
                list.add(JSON.parseObject(s,Wrong.class));
            }
            for(Wrong e:list){
                System.out.println(e.getExam().getEn());
                if(e.getExam().getCns().equals(InputUtil.getStr("请输入中文"))){
                    System.out.println("回答正确");
                    e.setNum(e.getNum()-1);
                    if(e.getNum()==0){
                        /*删除整个文件(太危险不删了),然后再写进去没有这个单词的文件(printwriter ) 嘿嘿嘿...*/
                    }
                }else{
                    System.out.println("回答错误");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
